/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             RegistryHelper.cs
*        创建系统时间:       2012/11/2 19:07:08
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System.Globalization;
       using Microsoft.Win32;

    public class RegistryHelper
    {
        public static string SubKey = @"Software\ShowLight";

        public static bool Exists(string key)
        {
            return Exists(SubKey, key);
        }

        public static bool Exists(string subKey, string key)
        {
            string[] subKeyNames = Registry.LocalMachine.OpenSubKey(subKey, false).GetSubKeyNames();
            for (int i = 0; i < subKeyNames.Length; i++)
            {
                if (key.Equals(subKeyNames[i]))
                {
                    return true;
                }
            }
            return false;
        }

        public static void GetConfig(BaseConfiguration configuration)
        {
            if (!Exists("Software", "ShowLight"))
            {
                SetValues(configuration);
            }
            else if (!Exists(configuration.SoftName))
            {
                SetValues(configuration);
            }
            if (configuration.DataBase.Length == 0)
            {
                GetValues(configuration);
            }
        }

        public static string GetValue(string key)
        {
            return (string)Registry.LocalMachine.OpenSubKey(SubKey, false).GetValue(key);
        }

        private static void GetValues(BaseConfiguration baseConfiguration)
        {
            SubKey = @"Software\ShowLight\" + baseConfiguration.SoftName;
            baseConfiguration.CustomerCompanyName = GetValue("CustomerCompanyName");
            baseConfiguration.ConfigurationFrom = baseConfiguration.GetConfiguration(GetValue("ConfigurationFrom"));
            baseConfiguration.SoftName = GetValue("SoftName");
            baseConfiguration.SoftFullName = GetValue("SoftFullName");
            baseConfiguration.RunMode = baseConfiguration.GetRunMode(GetValue("RunMode"));
            baseConfiguration.RootMenuID = GetValue("RootMenuID");
            baseConfiguration.CurrentLanguage = GetValue("CurrentLanguage");
            baseConfiguration.Version = GetValue("Version");
            baseConfiguration.CommonWebService = GetValue("CommonWebService");
            baseConfiguration.WebService = GetValue("WebService");
            baseConfiguration.WebServiceUsername = GetValue("WebServiceUsername");
            baseConfiguration.WebServicePassword = GetValue("WebServicePassword");
            baseConfiguration.DbConnectionString = GetValue("DbConnectionString");
            baseConfiguration.DataBaseType = baseConfiguration.GetDataBaseType(GetValue("DataBaseType"));
            baseConfiguration.ServerName = GetValue("ServerName");
            baseConfiguration.DataBase = GetValue("DataBase");
            baseConfiguration.TrustLink = string.Compare(GetValue("TrustLink"), "YES", true, CultureInfo.CurrentCulture) == 0;
            baseConfiguration.Username = GetValue("Username");
            baseConfiguration.Password = GetValue("Password");
            baseConfiguration.MaxPoolSize = int.Parse(GetValue("MaxPoolSize"), CultureInfo.CurrentCulture);
            baseConfiguration.MinPoolSize = int.Parse(GetValue("MinPoolSize"), CultureInfo.CurrentCulture);
            baseConfiguration.RegisterKey = GetValue("RegisterKey");
        }

        public static void SetValue(string key, string keyValue)
        {
            Registry.LocalMachine.OpenSubKey(SubKey, true).SetValue(key, keyValue);
        }

        private static void SetValues(BaseConfiguration configuration)
        {
            SubKey = @"Software\ShowLight\" + configuration.SoftName;
            SetValue("CustomerCompanyName", configuration.CustomerCompanyName);
            SetValue("ConfigurationFrom", configuration.RegisterKey.ToString());
            SetValue("SoftName", configuration.SoftName);
            SetValue("SoftFullName", configuration.SoftFullName);
            SetValue("RunMode", RunMode.Local.ToString());
            SetValue("RootMenuID", configuration.RootMenuID);
            SetValue("CurrentLanguage", configuration.CurrentLanguage);
            SetValue("CommonWebService", "www.ShowLight.com");
            SetValue("WebService", "www.ShowLight.com");
            SetValue("WebServiceUsername", "ShowLight");
            SetValue("WebServicePassword", "ShowLight");
            SetValue("DataBaseType", DataBaseType.Sqlserver.ToString());
            SetValue("ServerName", "127.0.0.1");
            SetValue("DataBase", configuration.SoftName);
            SetValue("TrustLink", "YES");
            SetValue("Username", "sa");
            SetValue("Password", "ShowLight");
            SetValue("MaxPoolSize", "5");
            SetValue("MinPoolSize", "2");
            SetValue("RegisterKey", "ShowLight");
        }
    }
}